//
//  YDYNetWorkTool.m
//  DY_Weibo3
//
//  Created by 杨德勇 on 2016/8/20.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYNetWorkTool.h"
#import "YDYUserAccount.h"
@implementation YDYNetWorkTool

+(instancetype)sharedTool{
    static YDYNetWorkTool* tool;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[YDYNetWorkTool alloc]initWithBaseURL:[NSURL URLWithString:ydy_baseURL]];
    });
    
    tool.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",nil];
    return tool;
}
-(void)loadStatus:(void (^)(NSArray *, NSError *))finished{
    
    YDYUserAccount *account = [YDYUserAccount sharedAccount];
    NSString *url = @"2/statuses/home_timeline.json";
    
    NSDictionary *parameters = @{@"access_token":account.access_token};
    
    [self GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *array = responseObject[@"statuses"];
        if (array.count != 0) {
            finished(array,nil);
        }else{
            NSError *error =[NSError errorWithDomain:@"2/statuses/home_timeline.json" code:10000 userInfo:@{@"text":@"返回数组为空"}];
            finished(nil,error);
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        finished(nil,error);
    }];

}
@end
